package crp.services.api.graphql.loaders;

import com.qwlabs.lang.C2;
import crp.services.api.graphql.models.AcademicTermPayload;
import crp.services.api.graphql.models.CourseAttributesPayload;
import crp.services.api.graphql.models.DepartmentPayload;
import crp.services.api.graphql.models.StorageObjectPayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Source;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.util.List;

@GraphQLApi
@RequestScoped
public class CourseAttributesPayloadLoader {
    private final DepartmentFetcher departmentFetcher;
    private final AcademicFetcher academicFetcher;

    @Inject
    public CourseAttributesPayloadLoader(DepartmentFetcher departmentFetcher,
                                         AcademicFetcher academicFetcher) {
        this.departmentFetcher = departmentFetcher;
        this.academicFetcher = academicFetcher;
    }

    public List<AcademicTermPayload> academicTerm(@Source List<CourseAttributesPayload> courseAttributes) {
        return academicFetcher.fetchTerms(courseAttributes, CourseAttributesPayload::getAcademicTermId);
    }

    public List<DepartmentPayload> department(@Source List<CourseAttributesPayload> courseAttributes) {
        return departmentFetcher.fetch(courseAttributes, CourseAttributesPayload::getOpenDepartmentId);
    }

    public List<StorageObjectPayload> cover(@Source List<CourseAttributesPayload> courseAttributes) {
        return C2.list(courseAttributes, attribute -> StorageObjectPayload.of(attribute.getCover()));
    }
}
